/*
 * @Descripttion:
 * @version:
 * @Author: Yang Zhang
 * @Date: 2021-11-18 22:40:55
 * @LastEditors: Yang Zhang
 * @LastEditTime: 2021-11-18 22:40:55
 */

import { createStore, useStore as baseUseStore, createLogger, Store } from 'vuex'
import getters from './getters'
import user from './modules/user'
import app from './modules/app'
import { IStore } from './types'
import { App, InjectionKey } from 'vue'

// 在开发环境中开启logger
const debug = process.env.NODE_ENV !== 'production'

export const key: InjectionKey<Store<IStore>> = Symbol()

const plugins = debug ? [createLogger({})] : []

const store = createStore<IStore>({
  plugins,
  modules: {
    user: {
      namespaced: true,
      ...user
    },
    app: {
      namespaced: true,
      ...app
    }
  },
  getters
})

// 定义你自己的“useStore”组合函数
export function useStore() {
  return baseUseStore(key)
}

export function setupStore(app: App) {
  app.use(store, key)
}

export default store
